Beginners guide to the std::sort() funct

您所在的位置:网站首页 sort of like that Beginners guide to the std::sort() funct

Beginners guide to the std::sort() funct

#Beginners guide to the std::sort() funct| 来源: 网络整理| 查看: 265

Important Information Now before we start I would like to state that I will be using features that are only available on C++11 compilers. If you don't have a C++11 or don't know if your compiler supports it, I would recommend doing this. Head on over to CodeBlocks and download their IDE. It comes with a C++11 compiler and you can enable it by going to settings->compiler->compiler settings->compiler flags-> and then you should see a checkbox that says something like Have g++ follow the C++11 ISO C++ language standard. Enable that and click ok and you should be good to go.

What It Looks Like The sort() function in the algorithm header can be a very useful tool to both new and experienced programmers. It's use is to sort containers like arrays and vectors.

The first example is what the function looks like. The second example is an optional overloaded function that includes a third parameter. First take a look at each of these functions and see if we can figure out what each parameter does.

Example 1 ~ std::sort(myvector.begin(), myvector.end())

Example 2 ~ std::sort(myvector.begin(), myvector.end(), myCompFunction)

About The Function So let鈥檚 go dig into these and figure out what each does and why it does it.

Found in ~ #include

Parameter 1 myvector.begin() ~ The first parameter is where you will be putting a iterator(Pointer) to the first element in the range that you want to sort. The sort will include the element that the iterator points to.

Parameter 2 myvector.end() ~ The second parameter is almost like the first but instead of putting a iterator to the first element to sort you will be putting a iterator to the last element. One very important difference is that the search won鈥檛 include the element that this iterator points to. It is [First,Last) meaning it includes the first parameter in the sort but it doesn鈥檛 include the second parameter in the sort.

Parameter 3 myCompFunction() Optional ~ I will only give a brief description here, because I will be explaining this parameter in more detail later. The third parameter is used to define how you do the search. For example if you have a struct that has 3 different variables in it, how does the function know which one to sort? Or how does it know how it should sort it? This is what this parameter is for. I will explain this more in a bit.

Function Return ~ This function doesn鈥檛 return anything because it alters the container directly through iterators(Pointers).

Array Example 12345678910111213141516171819202122 // sort() Example using arrays. // By Zereo 04/22/13 #include #include using namespace std; const int SIZE = 7; int main() { int intArray[SIZE] = {5, 3, 32, -1, 1, 104, 53}; //Now we call the sort function sort(intArray, intArray + SIZE); cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3